/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.menumanager.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Repository;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.menumanager.model.PlatMenu;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;
import net.sf.ehcache.CacheManager;

/**
 * ClassName:PlatMenMenuvice <br/>
 * Date: 2015年5月8日 下午4:28:49 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Repository("system.modules.system.PlatMenuService")
public class PlatMenuService extends BaseService {
	
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	@Transactional
	@CachePut(value="MenuCache",key="#menu.getMenuId()")
	public PlatMenu save(PlatMenu menu) {
		String id = menu.save();
		menu.setMenuId(id);
		return menu;
	}

	@Transactional
	@CachePut(value="MenuCache",key="#menu.getMenuId()")
	public PlatMenu update(PlatMenu menu) {
		return menu.update();
	}
	
	/**
	 * 只更新Menu基础字段  不会更新关联关系
	 * @param menu
	 * @return
	 */
	@Transactional
	@CachePut(value="MenuCache",key="#menu.getMenuId()")
	public PlatMenu updateMenuOnly(PlatMenu menu) {
		String hql = "update PlatMenu m set m.menuName=? , m.url=? , m.iconCls=? , m.enabled=? , m.menuSort=? where m.menuId=?";
		int result = this.hibernateDAO.executeUpdate(hql,menu.getMenuName(),menu.getUrl(),menu.getIconCls(),menu.getEnabled(),menu.getMenuSort(),menu.getMenuId());
		return result>0?menu:null;
	}

	@Transactional
	@CacheEvict(value="MenuCache",key="#menu.getMenuId()")
	public PlatMenu delete(PlatMenu menu) {
		return menu.delete();
	}

	@Transactional
	@CachePut(value="MenuCache",key="#menu.getMenuId()")
	public PlatMenu saveOrUpdate(PlatMenu menu) {
		if(menu.getMenuId() != null){
			return updateMenuOnly(menu);
		}
		return menu.saveOrUpdate();
	}
	
	@Transactional
	public List<PlatMenu> getAllMenus(){
		List<PlatMenu> menus = (List<PlatMenu>) super.getHibernateDAO().findAll("select u from PlatMenu as u order by u.menuSort");
		for(PlatMenu m : menus){
			PlatMenu parent = m.getParent_menu();
			if(parent != null){
				m.parent_id = parent.getMenuId();
			}
			//取消转换Json时自动查询
			m.setChildren(null);
			m.setResources(null);
			m.setBtnGrp(null);
		}
		return menus;
	}
	
	@Transactional
	public List<PlatMenu> getMenus(Set<PlatRole> roles){
		List params = new ArrayList();
		if(roles.size() == 0){
			return null;
		}
		String hql = "";
		for(PlatRole r : roles){
			hql+=("?,");
			params.add(r.getRoleId());
		}
		hql= hql.substring(0,hql.length()-1);
		
		List<PlatMenu> menus = (List<PlatMenu>) super.getHibernateDAO().findAll("select distinct u from PlatMenu as u , PlatRole as r where u.menuId in elements(r.menus) and r.id in ("+hql+") order by u.menuSort",params);
		
		for(PlatMenu m : menus){
			PlatMenu parent = m.getParent_menu();
			if(parent != null){
				m.parent_id = parent.getMenuId();
			}
			m.setChildren(null);
			m.setResources(null);
			m.setBtnGrp(null);
		}
		return menus;
	}

}
